package net.wsfcp.android.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import net.wsfcp.android.R;
import net.wsfcp.android.bean.resultbean.AddressBean;

import java.util.ArrayList;
//地址
public class AddressAdapter extends RecyclerView.Adapter<AddressAdapter.RecViewholder> {


    private Context context;
    private ArrayList<AddressBean> data = new ArrayList<>();
    private LayoutInflater layoutInflater;

    public AddressAdapter(Context context) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
    }

    public void setList(ArrayList<AddressBean> list) {
        data.clear();
        data.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public RecViewholder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.item_address, parent, false);
        return new RecViewholder(view);
    }

    @Override
    public void onBindViewHolder(RecViewholder holder, final int position) {
        AddressBean bean = data.get(position);

        if (bean.isDefault()) {
            holder.ll_top.setBackgroundResource(R.drawable.bg_address_top_color);
            holder.txt_default_address.setVisibility(View.VISIBLE);
//            holder.slide.setVisibility(View.GONE);

        } else {
            holder.ll_top.setBackgroundResource(R.drawable.bg_address_top_grey);
            holder.txt_default_address.setVisibility(View.GONE);
//            holder.slide.setVisibility(View.VISIBLE);

        }


        String myProvince = "";
        String myCity = "";
        String myDistrict = "";

        if (!TextUtils.isEmpty(bean.getProvince())) {
            myProvince = bean.getProvince();
        }
        if (!TextUtils.isEmpty(bean.getCity())) {
            myCity = bean.getCity();
        }
        if (!TextUtils.isEmpty(bean.getArea())) {
            myDistrict = bean.getArea();
        }


        holder.txt_name_phone.setText(bean.getReceiver() + "   " + bean.getCellNum());
        holder.txt_address.setText(myProvince + " " + myCity + " " + myDistrict + " " + bean.getDetailAddress());


    }

    @Override
    public int getItemCount() {
        return data.size();
    }


    public class RecViewholder extends RecyclerView.ViewHolder {
        //        @BindView(R.id.slide_itemView)
        public RelativeLayout slideItem;
        public LinearLayout ll_top;
        public TextView txt_default_address;
        public TextView txt_address;
        public TextView txt_name_phone;


//        private LinearLayout slide;

        public RecViewholder(View itemView) {
            super(itemView);
//            ButterKnife.inject(context,itemView);
            slideItem = itemView.findViewById(R.id.slide_itemView);
            ll_top = itemView.findViewById(R.id.ll_top);
            txt_default_address = itemView.findViewById(R.id.txt_default_address);
            txt_address = itemView.findViewById(R.id.txt_address);
            txt_name_phone = itemView.findViewById(R.id.txt_name_phone);
        }


    }


}
